package com.api.shopapi.mapper;

import com.api.shopapi.entity.UserFace;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface UserFaceMapper {

    @Insert("insert into user_face " +
            "(" +
            "UserFaceCode," +
            "UserCode," +
            "UserName,"+
            "GovernmentCode," +
            "InstitutionCode," +
            "FaceImg," +
            "FaceFeature" +
            ") " +
            "value " +
            "(" +
            "#{userFace.UserFaceCode}," +
            "#{userFace.UserCode}," +
            "#{userFace.UserName}," +
            "#{userFace.GovernmentCode}," +
            "#{userFace.InstitutionCode}," +
            "#{userFace.FaceImg}," +
            "#{userFace.FaceFeature})")
    int insert(@Param("userFace") UserFace userFace);

    @Delete("delete from user_face " +
            " where userFaceCode=#{userFaceCode}")
    int delete(@Param("userFaceCode") String userFaceCode);

    @Update("update user_face " +
            " set " +
            " UserName=#{userFace.UserName}," +
            " FaceImg=#{userFace.FaceImg}," +
            " FaceFeature=#{userFace.FaceFeature} " +
            " where " +
            " userFaceCode=#{userFace.userFaceCode}")
    int update(@Param("userFace") UserFace userFace);

    @Select("select  * from user_face where userFaceCode=#{userFaceCode} limit 1 ")
    UserFace getByUserFaceCode(@Param("userFaceCode") String userFaceCode);

    @Select("select  * from user_face where userCode=#{userCode} limit 1 ")
    UserFace getByUserCode(@Param("userCode") String userCode);

    @Select("<script>" +
            "select uf.* from user_face uf join user u on uf.UserCode=u.UserCode" +
            " where " +
            " uf.institutionCode = #{institutionCode}" +
            "<if test ='userType !=-1 '>" +
            " and u.UserType =#{userType}" +
            "</if>" +
            "<if test ='userName !=null and userName!=\"\" '>" +
            " and uf.UserName like concat('%',#{userName},'%') " +
            "</if>" +
            "</script>")
    List<UserFace> listByInsCode(@Param("institutionCode")String institutionCode,
                                 @Param("userName")String userName,
                                 @Param("userType")int userType);


    @Select("<script>" +
            "select uf.* from user_face uf join user u on uf.UserCode=u.UserCode" +
            " where " +
            " uf.governmentCode = #{governmentCode}" +
            "<if test ='userType !=-1 '>" +
            " and u.UserType =#{userType}" +
            "</if>" +
            "<if test ='userName !=null and userName!=\"\" '>" +
            " and uf.UserName like concat('%',#{userName},'%') " +
            "</if>" +
            "</script>")
    List<UserFace> listByGovCode(@Param("governmentCode")String governmentCode,
                                 @Param("userName")String userName,
                                 @Param("userType")int userType);


}
